//
//  CenterAlertView.swift
//  MT
//
//  Created by M on 2019/12/23.
//  Copyright © 2019 张强. All rights reserved.
//

import UIKit
import MTCategory

class CenterAlertView: BaseAlertView {
    
    private static var font: UIFont {
        return PingFangSCRegularFont(15)
    }
    private static var extraMargin: CGFloat {
        return 14.0
    }
    
    /// 展示信息
    var message: String {
        didSet {
            messageLabel.text = message
            setMessageLabelFrame()
        }
    }
    
    override var screenWidthRatio: CGFloat {
        didSet {
            setMessageLabelFrame()
        }
    }
    
    /// 信息 Label
    lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.font = CenterAlertView.font
        label.textColor = .white
        label.textAlignment = .center
        label.adjustsFontSizeToFitWidth = true
        label.numberOfLines = 0
        return label
    }()
    
    init(message: String) {
        self.message = message
        let maxWidth = ScreenWidth * 0.7 + CenterAlertView.extraMargin * 2
        let height = message.height(font: CenterAlertView.font, maxWidth: maxWidth) + CenterAlertView.extraMargin * 2
        super.init(height: height, popStyle: .center)
        setup()
        initializeUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

// MARK: ------------------ 初始化 ----------------------
extension CenterAlertView {
    
    private func setup() {
        backgroundColor = UIColor.black.withAlphaComponent(0.5)
        screenWidthRatio = 0.7
        shouldAutoDismiss = true
        layer.cornerRadius = 7
        isBackgroundCoverEnabled = false
        
    }
    
    private func initializeUI() {
        messageLabel.text = self.message
        addSubview(messageLabel)
        messageLabel.snp.makeConstraints { (make) in
            make.left.top.equalTo(self).offset(CenterAlertView.extraMargin)
            make.right.bottom.equalTo(self).offset(-CenterAlertView.extraMargin)
        }
    }
    
    private func setMessageLabelFrame() {
        let maxWidth = ScreenWidth * 0.7
        let height = message.height(font: CenterAlertView.font, maxWidth: maxWidth) + CenterAlertView.extraMargin * 2
        let width = message.width(font: CenterAlertView.font, maxHeight: CGFloat.greatestFiniteMagnitude)
        let w = (width > maxWidth ? maxWidth : width) + CenterAlertView.extraMargin * 2
        self.frame = CGRect(x: (ScreenWidth - w) / 2, y: (ScreenHeight - height) / 2, width: w, height: height)
    }
    
    override func present(animated: Bool = true, completion: ((Bool) -> Void)? = nil) {
        titleView.isHidden = title == nil
        titleLabel.isHidden = title == nil
        titleLabel.text = title
        
        setBackgroundCoverView()
        UIApplication.shared.keyWindow?.rootViewController?.view.addSubview(self)
        setMessageLabelFrame()
        UIView.animate(withDuration: animated ? duration : 0, animations: {
            if self.popStyle == .bottom {
                self.frame = CGRect(x: self.mt_x, y: ScreenHeight - self.mt_height, width: self.mt_width, height: self.mt_height)
            }
            self.alpha = 1
            self.backgroundCoverView?.alpha = 1
            
        }) { (flag) in
            if self.shouldAutoDismiss && self.popStyle == .center {
                DispatchQueue.main.asyncAfter(deadline: .now() + self.displayDuration) {
                    self.dismiss(animated: self.isDismissAnimated, completion: nil)
                }
            }
            if let completion = completion {
                completion(flag)
            }
        }
    }
    
}
